<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理器</title>
    <style>
        .completed {
            text-decoration: line-through;
            color: #999;
        }
        .delete-btn {
            color: red;
            margin-left: 10px;
            cursor: pointer;
        }

        /* 新增美化样式 */
        body {
            max-width: 600px;
            margin: 20px auto;
            padding: 0 20px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        #taskInput {
            padding: 10px;
            width: 60%;
            border: 2px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
        }

        #addBtn {
            padding: 10px 20px;
            background: #4CAF50;
            border: none;
            border-radius: 4px;
            color: white;
            cursor: pointer;
            transition: background 0.3s;
        }

        #addBtn:hover {
            background: #45a049;
        }

        #taskList {
            list-style: none;
            padding: 0;
            margin-top: 20px;
        }

        #taskList li {
            display: flex;
            align-items: center;
            padding: 12px;
            background: #f8f9fa;
            margin-bottom: 8px;
            border-radius: 4px;
            transition: all 0.3s;
        }

        #taskList li:hover {
            background: #e9ecef;
            transform: translateX(5px);
        }

        #taskList input[type="checkbox"] {
            width: 18px;
            height: 18px;
            margin-right: 12px;
            cursor: pointer;
        }

        #taskList span {
            flex-grow: 1;
            margin-right: 15px;
            font-size: 16px;
        }

        #taskList small {
            font-size: 12px;
            color: #6c757d;
            min-width: 120px;
        }

        .completed {
            color: #adb5bd !important;
            text-decoration: line-through;
        }

        .delete-btn {
            color: #dc3545;
            cursor: pointer;
            font-weight: bold;
            padding: 2px 8px;
            border-radius: 50%;
            transition: all 0.3s;
            opacity: 0;
        }

        #taskList li:hover .delete-btn {
            opacity: 1;
        }

        .delete-btn:hover {
            background: #ffe0e3;
        }
    </style>
</head>
<body>
    <h2>任务管理器</h2>
    <input type="text" id="taskInput" placeholder="请输入任务">
    <button id="addBtn">添加任务</button>
    <ul id="taskList"></ul>

    <script>
        var taskInput = document.getElementById("taskInput");
        var addBtn = document.getElementById("addBtn");
        var taskList = document.getElementById("taskList");
        var tasks = JSON.parse(localStorage.getItem('tasks')) || []; // 添加本地存储支持

        addBtn.addEventListener("click", addTask);

        function addTask() {
            var taskText = taskInput.value.trim();
            if (!taskText) return;
            
            var task = {
                text: taskText,
                created: new Date(),
                completed: false
            }
            tasks.push(task);
            taskInput.value = ""; // 清空输入框
            updateList();
        }

        function updateList() {
            taskList.innerHTML = "";
            
            tasks.forEach((task, index) => {
                var li = document.createElement("li");
                
                // 创建复选框
                var checkbox = document.createElement("input");
                checkbox.type = "checkbox";
                checkbox.checked = task.completed;
                checkbox.addEventListener("change", () => toggleTask(index));

                // 创建任务文本
                var span = document.createElement("span");
                span.textContent = task.text;
                span.className = task.completed ? "completed" : "";

                // 创建时间显示
                var time = document.createElement("small");
                time.textContent = task.created.toLocaleString();

                // 修改删除按钮创建部分
                var delBtn = document.createElement("span");
                delBtn.innerHTML = "×";
                delBtn.className = "delete-btn";
                delBtn.title = "删除任务";
                delBtn.addEventListener("click", () => deleteTask(index));

                // 组合元素
                li.appendChild(checkbox);
                li.appendChild(span);
                li.appendChild(time);
                li.appendChild(delBtn);

                taskList.appendChild(li);
            });

            localStorage.setItem('tasks', JSON.stringify(tasks)); // 保存到本地存储
        }

        function toggleTask(index) {
            tasks[index].completed = !tasks[index].completed;
            updateList();
        }

        function deleteTask(index) {
            if (confirm("确定要删除这个任务吗？")) {
                tasks.splice(index, 1);
                updateList();
            }
        }

        // 初始化时加载数据
        updateList();
    </script>
</body>
</html>